<?php


class NlpHorario 
{

	public function grillaHoraria($cadenaHorarios) {
	
	$frases = $cadenaHorarios;
	
	$frases = $this->normalizar($frases);
	$frases = $this->sinPuntiacion($frases);
	$frases = html_entity_decode($frases);
	$frases = preg_replace( "/(^\s+)|(\s+$)/us", "", $frases); 
	
	if(strlen($frases) == 0){
		return array();
	}
	
	$phrases = $this->splitFrase($frases);
	//var_dump( $phrases );
	
	$range = array();
	
	for($x = 1 ; $x < 7 ; $x++){
		$range['d='.$x]['d'] = '0:0';
		$range['d='.$x]['h'] = '0:0';
	}
	
	foreach ($phrases as $phrase ) {
		$range = $this->parsePhrase($phrase, $range);
	}
	
	return $range;
	
	}
	
	
	public function parsePhrase($frase, $range){
	
		$dd = explode(' de ',$frase);
	
		$dias = $this->getDayRange($dd[0]);
		$horas = $this->getHourRange($dd[1]);
		
		/*for($x = $dias['desde']; $x <= $dias['hasta'] ; $x++){
			for($y = $horas['desde']; $y <= $horas['hasta'] ; $y++){
				if($y == $horas['desde'] && $horas['desde_min'] > 0){
					$range['dia='.$x][$y] = $horas['desde_min'] ;
				} else if($y == $horas['hasta'] && $horas['hasta_min'] > 0){
					$range['dia='.$x][$y] = $horas['hasta_min'];
				} else {
					$range['dia='.$x][$y] = 60;
				}
			}
		}*/
		//echo 'días desde:'.  $dias['desde'] . ' hasta:'. $dias['hasta'] .' ('. $frase.')' . "\n";
			
		for($x = $dias['desde']; $x <= $dias['hasta'] ; $x++){
			$range['d='.$x]['d'] =  $horas['desde'] .':'. $horas['desde_min'];
			$range['d='.$x]['h'] =  $horas['hasta'] .':'. $horas['hasta_min'];	
			//echo '  '. $range['d='.$x]['d'] .'-'. $range['d='.$x]['h'] ."\n";
		}
		
		return $range;
	}
	
	
	public function getDayRange($dayRange){
	
		$dd = explode(' ',$dayRange);
		$rr = array();
		if(sizeof($dd) == 1) {
			$rr['desde'] = $this->parseDia($dd[0]);
			$rr['hasta'] = $this->parseDia($dd[0]);
		} else if(sizeof($dd) == 3) {
			$rr['desde'] = $this->parseDia($dd[0]);
			$rr['hasta'] = $this->parseDia($dd[2]);		
		} else {
			echo "error getDayRange";
		}
		return $rr;
	}
	
	public function getHourRange($hourRange){
	
		$dd = explode(' ',$hourRange);
		$rr = array();
		$rr['desde'] = 0;
		$rr['desde_min'] = 0;
		$rr['hasta'] = 0;
		$rr['hasta_min'] = 0;	
				
		if(sizeof($dd) == 1) {
			echo "error getHourRange - $hourRange";
		} else if(sizeof($dd) == 3) {
		
			$hd = $this->parseHour($dd[0]);
			if($hd > 99){
				$rr['desde'] = floor($hd/100);
				$rr['desde_min'] = $hd % 100;
			} else {
				$rr['desde'] = $hd;
				$rr['desde_min'] = 0;			
			}
			
			$hh = $this->parseHour($dd[2]);	
			if($hh > 99){
				$rr['hasta'] = floor($hh/100);
				$rr['hasta_min'] = $hh % 100;
			} else {
				$rr['hasta'] = $hh;
				$rr['hasta_min'] = 0;			
			}
	
		} else {
			echo "error getHourRange - $hourRange";
		}
		return $rr;
	}
	
	
	public function splitFrase($frases){
		
		$words = explode(' ',$frases);
		
		if(!$this->isDay($words[0])){
			return array();
		}
		
		$it = sizeof($words);
		$palant = '';
		$pal = '';
		$tramos = array();
		$itramos = 0;
		$tramo = '';
		for($i = 0; $i < $it; $i++) {
			$pal = $words[$i];
			if($this->isHour($palant) && $this->isDay($pal)) {
				//echo "SIII! tramo: $tramo ,pal: $pal ,palant: $palant \n";
				$tramos[$itramos] = trim($tramo);
				$itramos++;
				$tramo = '';
			}
			$tramo .= $pal . ' ';
			$palant = $pal;
		}
		if(sizeof($tramos) == 0){
			$tramos[0] = $frases;
		} else {
			$tramos[$itramos] = trim($tramo);
		}
		return $tramos;
	}
	
	
	
	
	
	
	public function isDay($word){
	if($this->parseDia($word) >-1){ return TRUE;}
	return FALSE;
	}
	
	public function isHour($word){
	if($this->parseHour($word) >-1){ return TRUE;}
	return FALSE;
	}
	
	public function isRangeMarker($word){
	if( strtolower($word) == "a" 	|| 
		strtolower($word) == "-"		||
		strtolower($word) == "hasta"	)
		{ return TRUE; }
	return FALSE;
	}
	
	
	public function isDimensionMarker($word) {
	if( strtolower($word) == "de" 	|| 
		strtolower($word) == ":"	||
		strtolower($word) == "en"	)
		{ return TRUE; }
	return FALSE;
	}
	
	public function parseDia($diaString) {
	$i = -1;
	
	if( strtolower($diaString) == "lun" 	|| 
		strtolower($diaString) == "lu"		||
		strtolower($diaString) == "lunes"	||
		strtolower($diaString) == "l")
		 { $i = 1; }
	if( strtolower($diaString) == "mar" 	|| 
		strtolower($diaString) == "ma"		||
		strtolower($diaString) == "martes"	||
		strtolower($diaString) == "m")
		 { $i = 2; }
	if( strtolower($diaString) == "mie" 		|| 
		strtolower($diaString) == "mier"		||
		strtolower($diaString) == "miércoles"	||
		strtolower($diaString) == "mi")
		 { $i = 3; }
	if( strtolower($diaString) == "jue" 	|| 
		strtolower($diaString) == "ju"		||
		strtolower($diaString) == "jueves"	||
		strtolower($diaString) == "j")
		 { $i = 4; }
	if( strtolower($diaString) == "vie" 	|| 
		strtolower($diaString) == "vi"		||
		strtolower($diaString) == "viernes"	||
		strtolower($diaString) == "v")
		 { $i = 5; }
	if( strtolower($diaString) == "say" 	|| 
		strtolower($diaString) == "sa"		||
		strtolower($diaString) == "sábado"	||
		strtolower($diaString) == "s")
		 { $i = 6; }
	if( strtolower($diaString) == "dom" 	|| 
		strtolower($diaString) == "do"		||
		strtolower($diaString) == "domingo"	||
		strtolower($diaString) == "d")
		 { $i = 7; }
	//echo "dia $diaString vale $i \n";
	return $i;
	
	}
	
	
	public function normalizar ($cadena){
	    $originales = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿŔŕ';
	    $modificadas = 'aaaaaaaceeeeiiiidnoooooouuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyRr';
	    $cadena = utf8_decode($cadena);
	    $cadena = strtr($cadena, utf8_decode($originales), $modificadas);
	    $cadena = strtolower($cadena);
	    $cadena = html_entity_decode($cadena);
	    $cadena = trim($cadena);
	    $cadena = preg_replace('/\s\s+/', ' ', $cadena);
	    return utf8_encode($cadena);
	}
	
	
	public function sinPuntiacion($word){
		$word = str_replace('.','', $word );
		$word = str_replace(',','', $word );
		$word = str_replace('!','', $word );
		$word = str_replace('?','', $word );
		return $word;
	}
	
	
	public function parseHour($word){
	$i = -1;
	$frac = 0;
	if(strlen($word) > 2){
		$frac = substr($word,-2);
		$word = substr($word,0,-2);
	}
	
	if( strtolower($word) == "0" 		|| 
		strtolower($word) == "0:00"		||
		strtolower($word) == "0hs"		||
		strtolower($word) == "12am")
		 { $i = 0; }
	if( strtolower($word) == "1" 		|| 
		strtolower($word) == "1:00"		||
		strtolower($word) == "1hs"		||
		strtolower($word) == "1h"		||
		strtolower($word) == "1am")
		 { $i = 1; }
	if( strtolower($word) == "2" 		|| 
		strtolower($word) == "2:00"		||
		strtolower($word) == "2hs"		||
		strtolower($word) == "2am")
		 { $i = 2; }
	if( strtolower($word) == "3" 		|| 
		strtolower($word) == "3:00"		||
		strtolower($word) == "3hs"		||
		strtolower($word) == "3am")
		 { $i = 3; }
	if( strtolower($word) == "4" 		|| 
		strtolower($word) == "4:00"		||
		strtolower($word) == "4hs"		||
		strtolower($word) == "4am")
		 { $i = 4; }
	if( strtolower($word) == "5" 		|| 
		strtolower($word) == "5:00"		||
		strtolower($word) == "5hs"		||
		strtolower($word) == "5am")
		 { $i = 5; }
	if( strtolower($word) == "6" 		|| 
		strtolower($word) == "6:00"		||
		strtolower($word) == "6hs"		||
		strtolower($word) == "6am")
		 { $i = 6; }
	if( strtolower($word) == "7" 		|| 
		strtolower($word) == "7:00"		||
		strtolower($word) == "7hs"		||
		strtolower($word) == "7am")
		 { $i = 7; }
	if( strtolower($word) == "8" 		|| 
		strtolower($word) == "8:00"		||
		strtolower($word) == "8hs"		||
		strtolower($word) == "8am")
		 { $i = 8; }
	if( strtolower($word) == "9" 		|| 
		strtolower($word) == "9:00"		||
		strtolower($word) == "9hs"		||
		strtolower($word) == "9am")
		 { $i = 9; }
	if( strtolower($word) == "10" 		|| 
		strtolower($word) == "10:00"	||
		strtolower($word) == "10hs"		||
		strtolower($word) == "10am")
		 { $i = 10; }
	if( strtolower($word) == "11" 		|| 
		strtolower($word) == "11:00"	||
		strtolower($word) == "11hs"		||
		strtolower($word) == "11am")
		 { $i = 11; }
	if( strtolower($word) == "12" 		|| 
		strtolower($word) == "12:00"	||
		strtolower($word) == "12hs"		||
		strtolower($word) == "12pm")
		 { $i = 12; }
	if( strtolower($word) == "13" 		|| 
		strtolower($word) == "13:00"	||
		strtolower($word) == "13hs"		||
		strtolower($word) == "1pm")
		 { $i = 13; }
	if( strtolower($word) == "14" 		|| 
		strtolower($word) == "14:00"	||
		strtolower($word) == "14hs"		||
		strtolower($word) == "2pm")
		 { $i = 14; }
	if( strtolower($word) == "15" 		|| 
		strtolower($word) == "15:00"	||
		strtolower($word) == "15hs"		||
		strtolower($word) == "3pm")
		 { $i = 15; }
	if( strtolower($word) == "16" 		|| 
		strtolower($word) == "16:00"	||
		strtolower($word) == "16hs"		||
		strtolower($word) == "4pm")
		 { $i = 16; }
	if( strtolower($word) == "17" 		|| 
		strtolower($word) == "17:00"	||
		strtolower($word) == "17hs"		||
		strtolower($word) == "5pm")
		 { $i = 17; }
	if( strtolower($word) == "18" 		|| 
		strtolower($word) == "18:00"	||
		strtolower($word) == "18hs"		||
		strtolower($word) == "6pm")
		 { $i = 18; }
	if( strtolower($word) == "19" 		|| 
		strtolower($word) == "19:00"	||
		strtolower($word) == "19hs"		||
		strtolower($word) == "7pm")
		 { $i = 19; }
	if( strtolower($word) == "20" 		|| 
		strtolower($word) == "20:00"	||
		strtolower($word) == "20hs"		||
		strtolower($word) == "8pm")
		 { $i = 20; }
	if( strtolower($word) == "21" 		|| 
		strtolower($word) == "21:00"	||
		strtolower($word) == "21hs"		||
		strtolower($word) == "9pm")
		 { $i = 21; }
	if( strtolower($word) == "22" 		|| 
		strtolower($word) == "22:00"	||
		strtolower($word) == "22hs"		||
		strtolower($word) == "10pm")
		 { $i = 22; }
	if( strtolower($word) == "23" 		|| 
		strtolower($word) == "23:00"	||
		strtolower($word) == "23hs"		||
		strtolower($word) == "11pm")
		 { $i = 23; }
	if( strtolower($word) == "24" 		|| 
		strtolower($word) == "24:00"	||
		strtolower($word) == "24hs"		||
		strtolower($word) == "12am")
		 { $i = 24; }
		
	//echo "hora $word vale $i \n";
	
	if($i > -1 && $frac != ''){
		$i = $i * 100;
		$i += intval($frac);
		//echo $i ."\n";
	}
	
	return $i;
	}


}